package com.tutos.ui.student;

import java.util.ArrayList;

import javax.inject.Inject;

import com.tutos.ui.MenuBase;
import com.tutos.ui.TutosApplication;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.VerticalLayout;

public class MenuStudent implements MenuBase, ClickListener {

	private static final long serialVersionUID = 1L;

	VerticalLayout layout = new VerticalLayout();
	ArrayList<MenuListener> list = new ArrayList<MenuBase.MenuListener>();

	private StudentMessages studentMessages;
	private EditStudent editStudent;
	private SearchTutoria searchTutoria;
	
	private Button logo;
	private Button buscar;
	private Button mensajes;
	private Button editar;
	private Button salir;

	@Inject
	public MenuStudent(StudentMessages studentMessages,
			EditStudent editStudent, SearchTutoria searchTutoria) {
		this.editStudent = editStudent;
		this.studentMessages = studentMessages;
		this.searchTutoria = searchTutoria;

		buildLayout();
	}

	@Override
	public void addListener(MenuListener listener) {
		if (listener == null)
			return;

		list.add(listener);
	}

	private void fireEvent(Component component) {
		for (MenuListener listener : list) {
			listener.menuClick(component);
		}
	}

	private void buildLayout() {

		logo = new Button();
		logo.setIcon(new ThemeResource("logo_menu.gif"));
		logo.setWidth("100%");
			
		buscar = new Button();
		buscar.addListener(this);
		buscar.setIcon(new ThemeResource("ibuscar.gif"));
		buscar.setDescription("Buscar Tutorias");
		
		mensajes = new Button();
		mensajes.addListener(this);
		mensajes.setIcon(new ThemeResource("imensajes.gif"));
		mensajes.setDescription("Tus Mensajes");
					
		editar = new Button();
		editar.addListener(this);
		editar.setIcon(new ThemeResource("ieditar.gif"));
		editar.setDescription("Editar tu informacion");
		
		salir = new Button();
		salir.addListener(this);
		salir.setIcon(new ThemeResource("isalir.gif"));
		salir.setDescription("Salir de Tutos");
		
		
		layout.addComponent(logo);
		layout.addComponent(buscar);
		layout.addComponent(mensajes);
		layout.addComponent(editar);
		layout.addComponent(salir);
	}

	@Override
	public Component getComponent() {
		return layout;
	}

	@Override
	public void buttonClick(ClickEvent event) {

		if (mensajes == event.getButton()) {
			fireEvent(studentMessages);
		} else if (buscar == event.getButton()) {
			fireEvent(searchTutoria.getComponent());
		} else if (editar == event.getButton()) {
			fireEvent(editStudent.getComponent());
		} else if (salir == event.getButton()) {
			TutosApplication.getInstance().close();
		}
	}

}
